package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.beans.TradeProvinceOrderAmount;
import com.atguigu.gmall.publisher.service.TradeStatsService;
import com.atguigu.gmall.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2023/6/20
 * 交易域统计controller
 * @Controller 将对象的创建交给Spring容器，如果类中方法返回的是String，处理完毕之后，会进行页面的跳转
 * 如果只是返回字符串，不进行页面跳转，可以在方法上加@ResponseBody
 * @RestController 将对象的创建交给Spring容器，如果类中方法返回的是String，处理完毕之后，不会进行页面的跳转
 * 只是将数据以字符串的形式返回给客户端。
 * @RestController = @Controller + @ResponseBody
 * @RequestMapping 拦截请求，将请求交给对应的方法进行处理
 * @RequestParam 接收请求中的参数 ，如果没有传递，可以给参数赋默认值
 */

@RestController
public class TradeStatsController {
    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatsService.getGMV(date);
        String json = "{\n" +
            "\t\t  \"status\": 0,\n" +
            "\t\t  \"data\": " + gmv + "\n" +
            "\t\t}\t";
        return json;
    }

    /*@RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceAmount = provinceAmountList.get(i);
            jsonB.append("{\"name\": \""+provinceAmount.getProvinceName()+"\",\"value\": "+provinceAmount.getOrderAmount()+"}");
            if(i < provinceAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }*/

    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }

        /*{
            "status": 0,
            "data": {
                "mapData": [
                {
                    "name": "北京",
                    "value": 8355
                },
                {
                    "name": "天津",
                    "value": 8805
                }
                ],
                "valueName": "交易额"
            }
        }*/
        Map resMap = new HashMap();
        resMap.put("status",0);
        Map dataMap = new HashMap();
        List mapDataList = new ArrayList();
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);
        for (TradeProvinceOrderAmount provinceOrderAmount : provinceAmountList) {
            Map map = new HashMap();
            map.put("name",provinceOrderAmount.getProvinceName());
            map.put("value",provinceOrderAmount.getOrderAmount());
            mapDataList.add(map);
        }
        dataMap.put("mapData",mapDataList);
        dataMap.put("valueName","交易额");

        resMap.put("data",dataMap);
        return resMap;
    }
}
